<?php

namespace app\common\model\mysql;

use think\Model;

class SGoodsGroupBuying extends Model
{
    /**
     * 查询拼团列表
     * @param $where
     * @param $field
     * @param $limit
     * @param $page
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getNormalGoodsGroupBuyingList($where, $field,$limit,$page){
        $order = [
            "a.group_buying_id" =>"desc",
        ];
        $result = $this->alias('a')->join('s_goods_group_user b', 'a.group_buying_id = b.group_buying_id')->join('s_goods_group c', 'a.goods_group_id = c.goods_group_id')->where($where)->field($field)->order($order)
            ->paginate([
                'list_rows' => $limit,
                'page' => $page,
            ]);
        return $result;
    }

    public function getGoodsGroupBuyingStatus($activityId,$field = '*'){
        if(empty($activityId)){
            return false;
        }
        $where = [
            ['group_buying_id','=',$activityId]
        ];
        $result = $this->where($where)->field($field)->find();
        return $result ? $result->toArray() : [];
    }

    /**
     * 查询拼团活动数量
     * @param $where
     * @return int
     * @throws \think\db\exception\DbException
     */
    public function getNormalGoodsGroupBuyingCount($where){
        $result = $this->where($where)->count();
        return $result;
    }

    /**
     * 查询拼团活动Ids数组
     * @param $where
     * @return array
     */
    public function getNormalGoodsGroupBuyingIds($where){
        $result = $this->where($where)->column("group_buying_id");
        return $result;
    }

    /**
     * 查询拼团列表
     * @param $where
     * @param string $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNormalGoodsGroupBuying($where, string $field = "*"){
        $result = $this->alias('a')->join('s_goods_group_user b', 'a.group_buying_id = b.group_buying_id')->where($where)->field($field)->select();
        return $result->toArray();
    }
}
